package com.loren.demo15.controller;

import com.loren.demo15.service.TaskService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/task")
public class TaskController {

    private final TaskService taskService;

    @GetMapping("/demo01")
    public String hello() throws InterruptedException {
        long start = System.currentTimeMillis();
        CompletableFuture<String> task1 = this.taskService.doTaskOne();
        log.info("---------1-----------");
        CompletableFuture<String> task2 = this.taskService.doTaskTwo();
        log.info("---------2-----------");
        CompletableFuture<String> task3 = this.taskService.doTaskThree();
        log.info("---------3-----------");
        CompletableFuture.allOf(task1, task2, task3).join();
        long end = System.currentTimeMillis();
        return "total time = " + (end - start);
    }
}
